package com.jn.servicedemo.views;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;


import com.jn.servicedemo.R;
import com.jn.servicedemo.constant.Constants;
import com.jn.servicedemo.service.CustomService;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private Button mStartButton;
    private Button mStopButton;
    private Button mBindButton;
    private Button mUnBindButton;
    private Button mNextButton;
    private Button mTestButton;
    private MyServiceConnection mConnection;
    private CustomService mService;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mStartButton = findViewById(R.id.start);
        mStopButton = findViewById(R.id.stop);
        mBindButton = findViewById(R.id.bind);
        mUnBindButton = findViewById(R.id.unbind);
        mNextButton = findViewById(R.id.next);
        mTestButton = findViewById(R.id.test);
        mStartButton.setOnClickListener(this);
        mStopButton.setOnClickListener(this);
        mBindButton.setOnClickListener(this);
        mUnBindButton.setOnClickListener(this);
        mNextButton.setOnClickListener(this);
        mTestButton.setOnClickListener(this);
        mConnection = new MyServiceConnection();
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.start) {
            Intent intent = new Intent(this, CustomService.class);
            intent.putExtra("command", Constants.COMMAND_PLAY);
            startService(intent);
            Intent aIntent = new Intent(this, MusicActivity.class);
            startActivity(aIntent);
        } else if (id == R.id.stop) {
            Intent intent = new Intent(this, CustomService.class);
            stopService(intent);
        } else if (id == R.id.bind) {
            Intent intent = new Intent(this, CustomService.class);
            bindService(intent, mConnection, BIND_AUTO_CREATE);
        } else if (id == R.id.unbind) {
            unbindService(mConnection);
        } else if (id == R.id.next) {
            Intent aIntent = new Intent(this, MusicActivity.class);
            startActivity(aIntent);
        } else if (id == R.id.test) {
            if (mService != null) {
                mService.next();
            }
        }
    }

    private class MyServiceConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("asdf", "=======onServiceConnected=======");
            mService = ((CustomService.MyBinder) service).getService();
            mService.play();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("asdf", "=======onServiceDisconnected=======");
            mService = null;
        }
    }
}